extends Button

@export var color: Globals.COIN_COLOR = 0
@onready var img: TextureRect = $img
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var score_label: Label = $scoreLabel
@onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	img.texture = Globals.coin_img[color]
	GameManager.update_coin_signal.connect(update_coin_num)
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

func _on_pressed() -> void:
	if GameManager.current_player != 0:
		return
	if GameManager.available_gems[color] == 0:
		return
	GameManager.available_gems[color] -= 1
	GameManager.players[0].save_coin[color] += 1
	if !ruleSystem.validate_gem_take(GameManager.players[0],GameManager.players[0].save_coin):
		GameManager.available_gems[color] += 1
		GameManager.players[0].save_coin[color] -= 1
		return
	audio_stream_player.play()
	GameManager.save_coin.emit()
	GameManager.update_opt_button.emit()
	update_coin_num()
	

func _on_mouse_entered() -> void:
	animation_player.play('hover')

func _on_mouse_exited() -> void:
	animation_player.play('RESET')
	
func update_coin_num() -> void:
	score_label.text = str(GameManager.available_gems[color])
